package com.open.capacity.reimburse.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.open.capacity.commons.Result;
import com.open.capacity.reimburse.dto.CalendarTipsDto;
import com.open.capacity.utils.DateUtil;
import io.swagger.annotations.Api;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.security.access.prepost.PreAuthorize;
import com.open.capacity.reimburse.entity.SysCalendarTips;
import com.open.capacity.reimburse.service.SysCalendarTipsService;

import com.open.capacity.commons.PageResult;

import javax.validation.Valid;

/**
 * 日历标签
 *
 * @author
 * @email
 * @date 2021-03-03 21:53:11
 */
@RestController
@RequestMapping("calendartips")
@Api(tags = "日历标签")
public class SysCalendarTipsController {

    @Autowired
    private SysCalendarTipsService sysCalendarTipsService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @PreAuthorize("hasAnyAuthority('sys_calendar_tips:syscalendartips:list')")
    public PageResult list(@RequestParam Map<String, Object> params) {
        PageResult pageResult = sysCalendarTipsService.pageAll(params);
        List<CalendarTipsDto> list = new ArrayList<>();
        for (SysCalendarTips item : (List<SysCalendarTips>) pageResult.getData()) {
            CalendarTipsDto dto = new CalendarTipsDto();
            dto.setId(item.getId());
            dto.setAllDay(false);
            dto.setStart(DateUtil.parse(DateUtil.formatDate(item.getStartDate()) + " " + item.getStartTime().substring(0, 2) + ":" + item.getStartTime().substring(2, 4) + ":00"));
            dto.setEnd(DateUtil.parse(DateUtil.formatDate(item.getEndDate()) + " " + item.getEndTime().substring(0, 2) + ":" + item.getEndTime().substring(2, 4) + ":00"));
            dto.setTitle(item.getContent());
            list.add(dto);
        }
        pageResult.setData(list);
        return pageResult;
    }

    /**
     * 查询单条记录
     */
    @RequestMapping("/findById")
    @PreAuthorize("hasAnyAuthority('sys_calendar_tips:syscalendartips:list')")
    public Result findById(String id) {
        return Result.succeed(sysCalendarTipsService.findById(id));
    }

    /**
     * 保存
     */
    @RequestMapping("/saveOrUpdate")
    @PreAuthorize("hasAnyAuthority('sys_calendar_tips:syscalendartips:save')")
    public Result save(@RequestBody @Valid SysCalendarTips sysCalendarTips, String keyValue) {
        if (StringUtils.isEmpty(keyValue)) {
            sysCalendarTipsService.save(sysCalendarTips);
        } else {
            sysCalendarTips.setId(keyValue);
            if(StringUtils.isEmpty(sysCalendarTips.getContent())){
                sysCalendarTipsService.delete(keyValue);
            }else {
                sysCalendarTipsService.update(sysCalendarTips);
            }
        }
        return Result.succeed("保存成功");
    }


    /**
     * 删除
     */
    @DeleteMapping("/delete/{id}")
    @PreAuthorize("hasAnyAuthority('sys_calendar_tips:syscalendartips:delete')")
    public Result delete(@PathVariable String id) {
        sysCalendarTipsService.delete(id);
        return Result.succeed("删除成功");
    }

}
